﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Doo_JetDAL;
using DoodJetView.Core;
using DoodJetView.Core.Client;
using System.Data.Objects;
using System.ComponentModel;
using System.Windows.Controls.Primitives;

namespace DoodJetView.VuesPartielles
{
    /// <summary>
    /// Logique d'interaction pour ClientForm.xaml
    /// </summary>
    public partial class ClientWindow : UserControl
    {
        public ClientWindow()
        {
            InitializeComponent();
            LoadDataForForm();

        }
        public ClientWindow(string nom, string prenom, string adresse, string adresse2, string ville, string tel, string dateNaiss, string permisCotier)
        {
        }
        public ClientWindow(client cli, string titre)
        {
            InitializeComponent();
            lbl_titre.Content = titre;
        }

        private void LoadDataForForm()
        {
            //Chargement des Activités 
            using (DataEntities db = new DataEntities())
            {
                    List<client> clients = db.client.ToList();
                    var x = new client();
                    foreach (client c in clients)
                    {
                        var Paco = new Client_poco { ID = (int)c.id_client, NomClient = c.nomClient + c.prenomClient };
                        this.listBoxClients.Items.Add(Paco);
                    }
               
            }
        }

        private void btnAfficher_Click(object sender, RoutedEventArgs e)
        {
            using (DataEntities cont = new DataEntities())
            {

                int IDClientList = ((DoodJetView.Core.Client.Client_poco)(listBoxClients.SelectedItem)).ID;
                try
                {
                    List<client> clients = cont.client.ToList();
                    foreach (client c in clients)
                    {
                        int idcli = int.Parse(c.id_client.ToString());
                        if (Equals(IDClientList, idcli))
                        {
                            var FormClient = new ClientForm(c, c.nomClient, "Show");
                            ListeClientGrid.Children.Clear();
                            ListeClientGrid.Children.Add(FormClient);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }

            }
        }

        private void btnAjouter_Click(object sender, RoutedEventArgs e)
        {
            var FormClient = new ClientForm();
            ListeClientGrid.Children.Clear();
            ListeClientGrid.Children.Add(FormClient);
        }

        private void btnSupprimer_Click(object sender, RoutedEventArgs e)
        {
            using (DataEntities cont = new DataEntities())
            {

                int IDClientList = ((DoodJetView.Core.Client.Client_poco)(listBoxClients.SelectedItem)).ID;
                try
                {
                    List<client> clients = cont.client.ToList();
                    foreach (client c in clients)
                    {
                        int idcli = int.Parse(c.id_client.ToString());
                        if (Equals(IDClientList, idcli))
                        {
                            cont.client.DeleteObject(c);
                            cont.SaveChanges();
                            cont.client.Where(o => o.nomClient == "123").ToList();
                            MessageBox.Show("Suppression :)", "ToDO", MessageBoxButton.OK);
                            listBoxClients.Items.Clear();
                            LoadDataForForm();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }

            }
        }

        private void recherche_Click(object sender, RoutedEventArgs e)
        {
            this.listBoxClients.Items.Clear();
            using (DataEntities db = new DataEntities())
            {
                try
                {
                    List<client> clients = db.client.ToList();
                    foreach (client c in clients)
                    {
                        var Paco = new Client_poco { ID = (int)c.id_client, NomClient = c.nomClient };
                        if (Paco.NomClient.ToUpper().IndexOf(this.textBox1.Text.ToUpper()) != -1)
                        {
                            this.listBoxClients.Items.Add(Paco);
                        }  
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
        }

        private void textBox1_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            this.listBoxClients.Items.Clear();
            using (DataEntities db = new DataEntities())
            {
                try
                {
                    List<client> clients = db.client.ToList();
                    foreach (client c in clients)
                    {
                        var Paco = new Client_poco { ID = (int)c.id_client, NomClient = c.nomClient };
                        if (Paco.NomClient.ToUpper().IndexOf(this.textBox1.Text.ToUpper()) != -1)
                        {
                            this.listBoxClients.Items.Add(Paco);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           try
            {
                int IDClientList = ((DoodJetView.Core.Client.Client_poco)(listBoxClients.SelectedItem)).ID;
                ClientForm FormClient = null;
                using (DataEntities db = new DataEntities())
                {
                    client res = db.client.SingleOrDefault(o => o.id_client == IDClientList);
                    FormClient = new ClientForm(res, res.prenomClient, "");
                }
                ListeClientGrid.Children.Clear();
                ListeClientGrid.Children.Add(FormClient);
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
    }
}
